package com.atguigu.boot04.config;

import com.atguigu.boot04.interceptor.LoginInterceptor;
import com.atguigu.boot04.interceptor.RedisUrlCountInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author:朱思博
 * @date:2021/5/8-{13:08}   SpringMVC配置类
 */
@Configuration
public class AdminWebConfig implements WebMvcConfigurer {

    @Autowired
    RedisUrlCountInterceptor interceptor;

    // 在配置类中添加拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/**")    // 需要拦截的请求 包括静态资源
                .excludePathPatterns("/","/login")  // 不拦截的请求 去登陆页
                .excludePathPatterns("/login")  // 登录也不拦截
                .excludePathPatterns("/admin")
                .excludePathPatterns("/css/**","/fonts/**","/images/**","/js/**") // 放行静态资源
        ;

        registry.addInterceptor(interceptor)
                .addPathPatterns("/","/login")
                .excludePathPatterns("/admin")
        ;
    }

    /*
        Filter和Interceptor的区别
            Filter：Servlet定义的原生组件，脱离Spring也能使用
            Interceptor：Spring定义的接口。可以使用Spring的自动装配功能
     */
}
